<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class GenerarMenu {

    var $CI;
    var $tables;

    function GenerarMenu() {
        $this->CI = & get_instance();
        $this->CI->load->config('crud_config', true);
        $this->tables = $this->CI->config->item('tables', 'crud_config');
        $this->CI->config->load('database_tables');
        $this->total_tables = $this->CI->config->item('dbtables');
    }
    
    function get_info_menu(){
        if($this->CI->db->table_exists($this->tables['tablas'])){
            $query_grupos = $this->CI->ion_auth->get_users_groups();
            $grupos = array();
            foreach($query_grupos->result_array() as $row_grupos){
                $grupos[] = $row_grupos['id'];
            }

            $this->CI->db->join($this->tables['tablas_grupos'], $this->tables['tablas'].'.id = '.$this->tables['tablas_grupos'].'.fk_id_'.$this->tables['tablas']);
            $this->CI->db->join($this->total_tables['menus'], $this->tables['tablas'].'.orden_menu_horizontal = '.$this->total_tables['menus'].'.id');
            $this->CI->db->order_by("orden_menu_horizontal", "asc");
            $this->CI->db->order_by("orden_menu_vertical", "asc");
            $this->CI->db->where_in("fk_id_groups", $grupos);
            $query = $this->CI->db->get($this->tables['tablas']);
            return $query;
        }
        return null;
    }

    function isMenuAccesible($opcion = null){
        if($opcion == null) return false;
        $query_grupos = $this->CI->ion_auth->get_users_groups();
        $grupos = array();
        foreach($query_grupos->result_array() as $row_grupos){
            $grupos[] = $row_grupos['id'];
        }

        $this->CI->db->join($this->tables['tablas_grupos'], $this->tables['tablas'].'.id = '.$this->tables['tablas_grupos'].'.fk_id_'.$this->tables['tablas']);
        $this->CI->db->where_in("fk_id_groups", $grupos);
        $this->CI->db->where("nombre", $opcion);
        $query = $this->CI->db->get($this->tables['tablas']);
        if($query->num_rows() > 0){
            return true;
        }
        return false;
    }
}

?>